package com.mylove.textprogressbar;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.mylove.textprogressbar.callback.IRefreshHead;
import com.mylove.textprogressbar.util.DensityUtils;

/**
 * @author myLove
 * @time 2017/12/29 17:09
 * @e-mail love@yanyi.red
 * @overview 默认头部view
 */

public class DefaultHeadView extends RelativeLayout implements IRefreshHead {
    private TextBarView barView;
    private int height = (int) DensityUtils.dipToPx(getContext(), 60);
    private float num1 = 0;
    private float num2 = 0;
    private float num3 = 0;
    private float size = 0;
    private int time = 0;
    private boolean boo = true;
    private boolean boo2 = true;

    public DefaultHeadView(Context context) {
        super(context);
        init(context);
    }

    public DefaultHeadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public DefaultHeadView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public DefaultHeadView(Context context, float num1, float num2, float num3, float size, int time) {
        super(context);
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
        this.size = size;
        this.time = time;
        init(context);
    }

    private void init(Context context) {
        View mView = LayoutInflater.from(context).inflate(R.layout.bar_layout, null);
        barView = mView.findViewById(R.id.bar);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, height);
        addView(mView, params);
    }

    public DefaultHeadView setSize(float size) {
        this.size = size;
        return this;
    }

    public DefaultHeadView setTime(int time) {
        this.time = time;
        return this;
    }

    public DefaultHeadView setNum(float num1, float num2, float num3) {
        this.num1 = num1;
        this.num2 = num2;
        this.num3 = num3;
        return this;
    }

    /**
     * 开始下拉
     */
    @Override
    public void onStart() {
        if (boo) {
            barView.setNum(num1, num2, num3)
                    .setSize(size)
                    .setTime(time)
                    .start();
        }
    }
    public DefaultHeadView setBoo(boolean boo){
        this.boo=boo;
        return this;
    }

    /**
     * 下拉中 多次被调用
     *
     * @param distance 头布局距离顶部的距离
     */
    @Override
    public void onPullDown(int distance) {
        if (boo2) {
            barView.setNum(num1, num2, num3)
                    .setSize(size)
                    .setTime(time)
                    .start();
            boo2 = false;
        }
    }

    /**
     * 松手 多次被调用
     *
     * @param distance 头布局距离顶部的距离
     */
    @Override
    public void onFingerUp(int distance) {
//        barView.setNum(num1, num2, num3)
//                .setSize(size)
//                .setTime(time)
//                .start();
    }

    /**
     * 结束
     */
    @Override
    public void onStop() {
        barView.stop();
        boo = false;
        boo2 = true;
    }

    @Override
    public int headViewHeight() {
        return height + 10;
    }
}
